################
#PSRM: Explaining Support for Redistribution: Social Insurance Systems and Fairness
#
#Experimental Data
#Part I: Generate dataframe
#
#Verena Fetscher
#July 2022
####################



##########################
#Import data
##########################

#Data files
#Decision-making part
#Name structure: year-month-day_hour-minute
#1707xx sessions run in Heidelberg Lab, others in Mannheim Lab
zTT<-zTreeTables(c("170330_1020.xls","170330_1126.xls","170403_0935.xls","170403_1144.xls","170425_1517.xls",
                   "170425_1334.xls","170425_1152.xls","170704_0915.xls","170704_1110.xls",
                   "170704_1251.xls"),c("globals","subjects","session","summary","contracts"))
session<-zTT$session

names(session) <- c("Date"     ,                "Treatment"             ,   "Subject"                 ,
                    "FinalProfit"    ,          "ShowUpFee"             ,   "ShowUpFeeInvested"       ,
                    "MoneyAdded"    ,           "MoneyToPay"            ,   "MoneyEarned"             ,
                    "total[1]"  ,               "total[2]"               ,  "total[3]"                ,
                    "total[4]"  ,               "total[5]"               ,  "total[6]"                ,
                    "total[7]"  ,               "total[8]"               ,  "total[9]"                ,
                    "total[10]" ,               "total[11]"              ,  "total[12]"               ,
                    "total[13]" ,               "total[14]"              ,  "total[15]"               ,
                    "total[16]" ,               "EndowmentDictator[1]"   ,  "EndowmentDictator[2]"    ,
                    "EndowmentDictator[3]" ,    "EndowmentDictator[4]"   ,  "EndowmentDictator[5]"    ,
                    "EndowmentDictator[6]" ,    "EndowmentDictator[7]"   ,  "EndowmentDictator[8]"    ,
                    "EndowmentDictator[9]" ,    "EndowmentDictator[10]"  ,  "EndowmentDictator[11]"   ,
                    "EndowmentDictator[12]",    "EndowmentDictator[13]"  ,  "EndowmentDictator[14]"   ,
                    "EndowmentDictator[15]",    "EndowmentDictator[16]"  ,  "EndowmentRecipient[1]"   ,
                    "EndowmentRecipient[2]" ,   "EndowmentRecipient[3]"  ,  "EndowmentRecipient[4]"   ,
                    "EndowmentRecipient[5]" ,   "EndowmentRecipient[6]"  ,  "EndowmentRecipient[7]"   ,
                    "EndowmentRecipient[8]" ,   "EndowmentRecipient[9]"  ,  "EndowmentRecipient[10]"  ,
                    "EndowmentRecipient[11]",   "EndowmentRecipient[12]" ,  "EndowmentRecipient[13]"  ,
                    "EndowmentRecipient[14]",   "EndowmentRecipient[15]" ,  "EndowmentRecipient[16]"  ,
                    "RiskDictator[1]" ,         "RiskDictator[2]"        ,  "RiskDictator[3]"         ,
                    "RiskDictator[4]" ,         "RiskDictator[5]"        ,  "RiskDictator[6]"         ,
                    "RiskDictator[7]" ,         "RiskDictator[8]"        ,  "RiskDictator[9]"         ,
                    "RiskDictator[10]",         "RiskDictator[11]"       ,  "RiskDictator[12]"        ,
                    "RiskDictator[13]",         "RiskDictator[14]"       ,  "RiskDictator[15]"        ,
                    "RiskDictator[16]",         "RiskRecipient[1]"       ,  "RiskRecipient[2]"        ,
                    "RiskRecipient[3]" ,        "RiskRecipient[4]"       ,  "RiskRecipient[5]"        ,
                    "RiskRecipient[6]" ,        "RiskRecipient[7]"       ,  "RiskRecipient[8]"        ,
                    "RiskRecipient[9]" ,        "RiskRecipient[10]"      ,  "RiskRecipient[11]"       ,
                    "RiskRecipient[12]",        "RiskRecipient[13]"      ,  "RiskRecipient[14]"       ,
                    "RiskRecipient[15]",        "RiskRecipient[16]"      ,  "ReplacementDictator[1]"  ,
                    "ReplacementDictator[2]" ,  "ReplacementDictator[3]" ,  "ReplacementDictator[4]"  ,
                    "ReplacementDictator[5]" ,  "ReplacementDictator[6]" ,  "ReplacementDictator[7]"  ,
                    "ReplacementDictator[8]" ,  "ReplacementDictator[9]" ,  "ReplacementDictator[10]" ,
                    "ReplacementDictator[11]",  "ReplacementDictator[12]" , "ReplacementDictator[13]" ,
                    "ReplacementDictator[14]",  "ReplacementDictator[15]" , "ReplacementDictator[16]" ,
                    "ReplacementRecipient[1]" , "ReplacementRecipient[2]" , "ReplacementRecipient[3]" ,
                    "ReplacementRecipient[4]" , "ReplacementRecipient[5]" , "ReplacementRecipient[6]" ,
                    "ReplacementRecipient[7]" , "ReplacementRecipient[8]" , "ReplacementRecipient[9]" ,
                    "ReplacementRecipient[10]", "ReplacementRecipient[11]", "ReplacementRecipient[12]",
                    "ReplacementRecipient[13]", "ReplacementRecipient[14]", "ReplacementRecipient[15]",
                    "ReplacementRecipient[16]", "give[1]"                 , "give[2]"                 ,
                    "give[3]"  ,                "give[4]"                 , "give[5]"                 ,
                    "give[6]"  ,                "give[7]"                 , "give[8]"                 ,
                    "give[9]"  ,                "give[10]"                , "give[11]"                ,
                    "give[12]" ,                "give[13]"                , "give[14]"                ,
                    "give[15]" ,                "give[16]"                , "transfer[1]"             ,
                    "transfer[2]"  ,            "transfer[3]"             , "transfer[4]"             ,
                    "transfer[5]"  ,            "transfer[6]"             , "transfer[7]"             ,
                    "transfer[8]"  ,            "transfer[9]"             , "transfer[10]"            ,
                    "transfer[11]" ,            "transfer[12]"            , "transfer[13]"            ,
                    "transfer[14]" ,            "transfer[15]"            , "transfer[16]"            ,
                    "winDictator[1]" ,          "winDictator[2]"          , "winDictator[3]"          ,
                    "winDictator[4]" ,          "winDictator[5]"          , "winDictator[6]"          ,
                    "winDictator[7]" ,          "winDictator[8]"          , "winDictator[9]"          ,
                    "winDictator[10]",          "winDictator[11]"         , "winDictator[12]"         ,
                    "winDictator[13]",          "winDictator[14]"         , "winDictator[15]"         ,
                    "winDictator[16]",          "winRecipient[1]"         , "winRecipient[2]"         ,
                    "winRecipient[3]" ,         "winRecipient[4]"         , "winRecipient[5]"         ,
                    "winRecipient[6]" ,         "winRecipient[7]"         , "winRecipient[8]"         ,
                    "winRecipient[9]" ,         "winRecipient[10]"        , "winRecipient[11]"        ,
                    "winRecipient[12]",         "winRecipient[13]"        , "winRecipient[14]"        ,
                    "winRecipient[15]",         "winRecipient[16]"        , "ProfitDictator[1]"       ,
                    "ProfitDictator[2]" ,       "ProfitDictator[3]"       , "ProfitDictator[4]"       ,
                    "ProfitDictator[5]" ,       "ProfitDictator[6]"       , "ProfitDictator[7]"       ,
                    "ProfitDictator[8]" ,       "ProfitDictator[9]"       , "ProfitDictator[10]"      ,
                    "ProfitDictator[11]",       "ProfitDictator[12]"      , "ProfitDictator[13]"      ,
                    "ProfitDictator[14]",       "ProfitDictator[15]"      , "ProfitDictator[16]"      ,
                    "ProfitRecipient[1]" ,      "ProfitRecipient[2]"      , "ProfitRecipient[3]"      ,
                    "ProfitRecipient[4]" ,      "ProfitRecipient[5]"      , "ProfitRecipient[6]"      ,
                    "ProfitRecipient[7]" ,      "ProfitRecipient[8]"      , "ProfitRecipient[9]"      ,
                    "ProfitRecipient[10]",      "ProfitRecipient[11]"     , "ProfitRecipient[12]"     ,
                    "ProfitRecipient[13]",      "ProfitRecipient[14]"     , "ProfitRecipient[15]"     ,
                    "ProfitRecipient[16]",      "Round[1]"                , "Round[2]"                ,
                    "Round[3]"  ,               "Round[4]"                , "Round[5]"                ,
                    "Round[6]"  ,               "Round[7]"                , "Round[8]"                ,
                    "Round[9]"  ,               "Round[10]"               , "Round[11]"               ,
                    "Round[12]" ,               "Round[13]"               , "Round[14]"               ,
                    "Round[15]" ,               "Round[16]"               , "rdmRound[1]"             ,
                    "rdmRound[2]" ,             "rdmRound[3]"             , "rdmRound[4]"             ,
                    "rdmRound[5]" ,             "rdmRound[6]"             , "rdmRound[7]"             ,
                    "rdmRound[8]" ,             "rdmRound[9]"             , "rdmRound[10]"            ,
                    "rdmRound[11]",             "rdmRound[12]"            , "rdmRound[13]"            ,
                    "rdmRound[14]",             "rdmRound[15]"            , "rdmRound[16]"            ,
                    "Role[1]"  ,                "Role[2]"                 , "Role[3]"                 ,
                    "Role[4]"  ,                "Role[5]"                 , "Role[6]"                 ,
                    "Role[7]"  ,                "Role[8]"                 , "Role[9]"                 ,
                    "Role[10]" ,                "Role[11]"                , "Role[12]"                ,
                    "Role[13]" ,                "Role[14]"                , "Role[15]"                ,
                    "Role[16]" ,                "Group[1]"                , "Group[2]"                ,
                    "Group[3]"  ,               "Group[4]"                , "Group[5]"                ,
                    "Group[6]"  ,               "Group[7]"                , "Group[8]"                ,
                    "Group[9]"  ,               "Group[10]"               , "Group[11]"               ,
                    "Group[12]" ,               "Group[13]"               , "Group[14]"               ,
                    "Group[15]" ,               "Group[16]"               , "numClicks[1]"            ,
                    "numClicks[2]" ,            "numClicks[3]"            , "numClicks[4]"            ,
                    "numClicks[5]" ,            "numClicks[6]"            , "numClicks[7]"            ,
                    "numClicks[8]" ,            "numClicks[9]"            , "numClicks[10]"           ,
                    "numClicks[11]",            "numClicks[12]"           , "numClicks[13]"           ,
                    "numClicks[14]",            "numClicks[15]"           , "numClicks[16]"           ,
                    "rdmNumRisk[1]",            "rdmNumRisk[2]"           , "rdmNumRisk[3]"           ,
                    "rdmNumRisk[4]" ,           "rdmNumRisk[5]"           , "rdmNumRisk[6]"           ,
                    "rdmNumRisk[7]"  ,          "rdmNumRisk[8]"           , "rdmNumRisk[9]"           ,
                    "risk_decision[1]" ,        "risk_decision[2]"        , "risk_decision[3]"        ,
                    "risk_decision[4]" ,        "risk_decision[5]"        , "risk_decision[6]"        ,
                    "risk_decision[7]" ,        "risk_decision[8]"        , "risk_decision[9]"        ,
                    "risk_outcome[1]" ,         "risk_outcome[2]"         , "risk_outcome[3]"         ,
                    "risk_outcome[4]" ,         "risk_outcome[5]"         , "risk_outcome[6]"         ,
                    "risk_outcome[7]" ,         "risk_outcome[8]"         , "risk_outcome[9]"         ,
                    "Rate"     ,                "numRounds"               , "i"                       ,
                    "counter"     ,             "numRoundsRisk"           , "p"                       ,
                    "j"     ,                   "k"                       , "pay_dictator"            ,
                    "pay_recipient"    ,        "pay_risk"  )

#Generate subject ID
session$ID<-c(1:length(session$Subject))


#Questionaire part
files <- list.files(pattern = "*.sbj$",recursive=TRUE)

sbj <- zTreeSbj(files)
names(sbj) <- c("Date","Subject","client","age","major",
                "gender","religion","rel_group","income_fam","polintr",
                "leftright","party","stfgov","gincdif","equalopt",
                "sucess","instr","change","changes_reason")

sbj$ID<-c(1:length(session$Subject))

s<-merge(session,sbj,by="ID")
##########################




##########################
#Change data structure
##########################

#Total endowment
names(s)
s[,11]

for(i in 1:16) { 
  nam <- paste("total", i, sep = "_")
  assign(nam, s[,10+i]) ##start with -1
}

s[,10+1]
s$`total[1]`
total_1
##########################


#Endowment dictator
names(s)
s[,27]

for(i in 1:16) { 
  nam <- paste("EndowmentDictator", i, sep = "_")
  assign(nam, s[,26+i]) ##start with -1
}

s$`EndowmentDictator[16]`
EndowmentDictator_16
##########################


#Endowment recipient
names(s)
s[,43]

for(i in 1:16) { 
  nam <- paste("EndowmentRecipient", i, sep = "_")
  assign(nam, s[,42+i]) ##start with -1
}

s$`EndowmentRecipient[16]`
EndowmentRecipient_16
##########################


#Risk dictator
names(s)
s[,59]

for(i in 1:16) { 
  nam <- paste("RiskDictator", i, sep = "_")
  assign(nam, s[,58+i]) ##start with -1
}

s$`RiskDictator[11]`
RiskDictator_11
##########################


#Risk recipient
names(s)
s[,75]

for(i in 1:16) { 
  nam <- paste("RiskRecipient", i, sep = "_")
  assign(nam, s[,74+i]) ##start with -1
}

s$`RiskRecipient[16]`
RiskRecipient_16
##########################


#Replacement dictator
names(s)
s[,91]

for(i in 1:16) { 
  nam <- paste("ReplacementDictator", i, sep = "_")
  assign(nam, s[,90+i]) ##start with -1
}

s$`ReplacementDictator[15]`
ReplacementDictator_15
##########################


#Replacement recipient
names(s)
s[,107]

for(i in 1:16) { 
  nam <- paste("ReplacementRecipient", i, sep = "_")
  assign(nam, s[,106+i]) ##start with -1
}

s$`ReplacementRecipient[12]`
ReplacementRecipient_12
##########################


#Win lottery dictator
names(s)
s[,155]

for(i in 1:16) { 
  nam <- paste("winDictator", i, sep = "_")
  assign(nam, s[,154+i]) ##start with -1
}

s$`winDictator[16]`
winDictator_16
##########################


#Win lottery recipient
names(s)
s[,171]

for(i in 1:16) { 
  nam <- paste("winRecipient", i, sep = "_")
  assign(nam, s[,170+i]) ##start with -1
}

s$`winRecipient[16]`
winRecipient_16
##########################


#Give
names(s)
s[,171]

for(i in 1:16) { 
  nam <- paste("give", i, sep = "_")
  assign(nam, s[,122+i]) ##start with -1
}

s$`give[16]`
give_16
##########################


#Transfer decision
names(s)
s[,139]

for(i in 1:16) { 
  nam <- paste("transfer", i, sep = "_")
  assign(nam, s[,138+i]) ##start with -1
}

#Test
s$`transfer[1]`
transfer_1
##########################


#Number clicks on slider
names(s)
s[,283]

for(i in 1:16) { 
  nam <- paste("numClicks", i, sep = "_")
  assign(nam, s[,282+i]) ##start with -1
}

s$`numClicks[16]`
numClicks_16
##########################


#Random round
names(s)
s[,235]

for(i in 1:16) { 
  nam <- paste("round", i, sep = "_")
  assign(nam, s[,234+i]) ##start with -1
}

#Test
s$`rdmRound[15]`
round_15
##########################


#Risk aversion
names(s)
s[,308]
s[,308+8]

for(i in 1:9) { 
  nam <- paste("risk", i, sep = "_")
  assign(nam, s[,307+i])
}

#Test
s$`risk_decision[9]`
risk_9
##########################


table(sbj$major)

sbj$major_code<-sbj$major

sbj$major_code[sbj$major=="Jura"|sbj$major=="jura"|sbj$major=="Unternehmensjura"
               |sbj$major=="Unternehmensjurist"
               |sbj$major=="Unternehmensjurist LLB"|
                 sbj$major=="Rechtswissenschaften"|sbj$major=="Unternehmensjura"]<-"Jura"

sbj$major_code[sbj$major=="VWL"|sbj$major=="vwl"|sbj$major=="Economics"
               |sbj$major=="Volkswirtschaftslehre"|sbj$major=="VWL mit Beifach Psychologie"]<-"VWL"

sbj$major_code[sbj$major=="BWL"|sbj$major=="Betriebswirtschaftslehre"
               |sbj$major=="Management Master"|sbj$major=="MMM"
               |sbj$major=="Mannheim Master of Management"|sbj$major=="Master in Management"
               |sbj$major=="BaKuWi"]<-"BWL"

sbj$major_code[sbj$major=="Wip?d"|sbj$major=="Wirtschaftsp?daogik"
               |sbj$major=="Wirtschaftsp?dagogik"]<-"Wirtschaftsp?dagogik"

sbj$major_code[sbj$major=="Wirtschaftsinformatik"|sbj$major=="Wifo"
               |sbj$major=="WiMa"
               |sbj$major=="Wirtschaftsmathe"]<-"Wirtschaftsinformatik"

sbj$major_code[sbj$major=="Humanmedizin"|sbj$major=="Medizin"
               |sbj$major=="Pharmazie"]<-"Medizin"

sbj$major_code[sbj$major=="Soziologie "|sbj$major=="Soziologie"]<-"Soziologie"


table(sbj$major_code)
##########################


##########################
#Generate data frame
##########################

dat<-cbind.data.frame(s$ID,s$Date.x,s$`Role[1]`,
                         total_1,total_2, total_3,total_4,total_5,total_6,total_7,
                         total_8,total_9,total_10,total_11,total_12,total_13,
                         total_14,total_15,total_16,
                         EndowmentDictator_1,EndowmentDictator_2, EndowmentDictator_3,EndowmentDictator_4,
                         EndowmentDictator_5,EndowmentDictator_6,EndowmentDictator_7,
                         EndowmentDictator_8,EndowmentDictator_9,EndowmentDictator_10,EndowmentDictator_11,
                         EndowmentDictator_12,EndowmentDictator_13,
                         EndowmentDictator_14,EndowmentDictator_15,EndowmentDictator_16,
                         EndowmentRecipient_1,EndowmentRecipient_2, EndowmentRecipient_3,EndowmentRecipient_4,EndowmentRecipient_5,EndowmentRecipient_6,EndowmentRecipient_7,
                         EndowmentRecipient_8,EndowmentRecipient_9,EndowmentRecipient_10,EndowmentRecipient_11,EndowmentRecipient_12,EndowmentRecipient_13,
                         EndowmentRecipient_14,EndowmentRecipient_15,EndowmentRecipient_16,
                         RiskDictator_1,RiskDictator_2, RiskDictator_3,RiskDictator_4,RiskDictator_5,RiskDictator_6,RiskDictator_7,
                         RiskDictator_8,RiskDictator_9,RiskDictator_10,RiskDictator_11,RiskDictator_12,RiskDictator_13,
                         RiskDictator_14,RiskDictator_15,RiskDictator_16,
                         RiskRecipient_1,RiskRecipient_2, RiskRecipient_3,RiskRecipient_4,RiskRecipient_5,RiskRecipient_6,RiskRecipient_7,
                         RiskRecipient_8,RiskRecipient_9,RiskRecipient_10,RiskRecipient_11,RiskRecipient_12,RiskRecipient_13,
                         RiskRecipient_14,RiskRecipient_15,RiskRecipient_16,
                         ReplacementDictator_1,ReplacementDictator_2, ReplacementDictator_3,ReplacementDictator_4,ReplacementDictator_5,ReplacementDictator_6,ReplacementDictator_7,
                         ReplacementDictator_8,ReplacementDictator_9,ReplacementDictator_10,ReplacementDictator_11,ReplacementDictator_12,ReplacementDictator_13,
                         ReplacementDictator_14,ReplacementDictator_15,ReplacementDictator_16,
                         ReplacementRecipient_1,ReplacementRecipient_2, ReplacementRecipient_3,ReplacementRecipient_4,ReplacementRecipient_5,ReplacementRecipient_6,ReplacementRecipient_7,
                         ReplacementRecipient_8,ReplacementRecipient_9,ReplacementRecipient_10,ReplacementRecipient_11,ReplacementRecipient_12,ReplacementRecipient_13,
                         ReplacementRecipient_14,ReplacementRecipient_15,ReplacementRecipient_16,
                         winDictator_1,winDictator_2, winDictator_3,winDictator_4,winDictator_5,winDictator_6,winDictator_7,
                         winDictator_8,winDictator_9,winDictator_10,winDictator_11,winDictator_12,winDictator_13,
                         winDictator_14,winDictator_15,winDictator_16,
                         winRecipient_1,winRecipient_2, winRecipient_3,winRecipient_4,winRecipient_5,winRecipient_6,winRecipient_7,
                         winRecipient_8,winRecipient_9,winRecipient_10,winRecipient_11,winRecipient_12,winRecipient_13,
                         winRecipient_14,winRecipient_15,winRecipient_16,
                         give_1,give_2, give_3,give_4,give_5,give_6,give_7,
                         give_8,give_9,give_10,give_11,give_12,give_13,
                         give_14,give_15,give_16,
                         transfer_1,transfer_2,transfer_3,transfer_4,transfer_5,transfer_6,transfer_7,transfer_8,
                         transfer_9,transfer_10,transfer_11,transfer_12,transfer_13,transfer_14,
                         transfer_15,transfer_16,
                         numClicks_1,numClicks_2, numClicks_3,numClicks_4,numClicks_5,numClicks_6,numClicks_7,
                         numClicks_8,numClicks_9,numClicks_10,numClicks_11,numClicks_12,numClicks_13,
                         numClicks_14,numClicks_15,numClicks_16,
                         round_1,round_2,round_3,round_4,round_5,round_6,round_7,
                         round_8, round_9, round_10,round_11,round_12,round_13,round_14,round_15,round_16,
                         risk_1,risk_2,risk_3,risk_4,risk_5,risk_6,risk_7,risk_8,risk_9,
                         s$age,s$major,s$gender,s$religion,s$rel_group,
                         s$income_fam,s$polintr,s$leftright,s$party,s$stfgov,
                         s$gincdif,s$equalopt,s$sucess,s$instr,s$change,s$changes_reason)

dat$`s$changes_reason`
s$`EndowmentDictator[10]`




##########################
#Transfor data frame to to cross-time structure
##########################

names(dat)
dat_long<-reshape(dat, varying = c(4:211)
                  , direction="long", idvar=c("`s$ID`"), 
                  sep="_",timevar = "order")
dat$`s$ID`
dat_long<-dat_long[order(dat_long$`s$ID`),]


#s$age,s$major,s$gender,s$religion,s$rel_group,s$income_fam,s$polintr,s$leftright,s$party,s$stfgov,
#s$gincdif,s$equalopt,s$sucess,s$instr,s$change,s$changes_reason


names(dat_long)
table(dat_long$`s$gender`)
dat$change
dat_long$`s$Date.x`

#Change colnames
colnames(dat_long) <- c("ID","Session","Role","risk_1","risk_2","risk_3","risk_4","risk_5"
                        ,"risk_6","risk_7","risk_8","risk_9","age","major","gender","religion",
                        "rel_group","income_fam","polintr","leftright","party","satisfy_gov",
                        "redistribution","equal_opp","success","instructions","change","change_reason",
                        "order","total","EndowmentDictator","EndowmentRecipient","RiskDictator",
                        "RiskRecipient","ReplacementDictator","ReplacementRecipient","winDictator",
                        "winRecipient","give","transfer","numclicks","round","ID2")

table(dat_long$gender)
##########################

rm(dat)


##########################
#Recode data
##########################

#Expected Endowment
dat_long$EU_dict<-(1-dat_long$RiskDictator)*dat_long$EndowmentDictator+
  dat_long$RiskDictator*dat_long$ReplacementDictator*dat_long$EndowmentDictator

dat_long$EU_recipient<-(1-dat_long$RiskRecipient)*dat_long$EndowmentRecipient+
  dat_long$RiskRecipient*dat_long$ReplacementRecipient*dat_long$EndowmentRecipient
##########################


#Decision situation
dat_long$situation<-NA

dat_long$situation[dat_long$total==4700&dat_long$RiskRecipient==.2&dat_long$RiskDictator==.2&
                     dat_long$ReplacementRecipient==.2&dat_long$ReplacementDictator==.8]<-1

dat_long$situation[dat_long$total==4700&dat_long$RiskRecipient==.2&dat_long$RiskDictator==.2&
                     dat_long$ReplacementRecipient==.8&dat_long$ReplacementDictator==.8]<-2

dat_long$situation[dat_long$total==4700&dat_long$RiskRecipient==0&dat_long$RiskDictator==0&
                     dat_long$ReplacementRecipient==1&dat_long$ReplacementDictator==1]<-3

dat_long$situation[dat_long$total==9400&dat_long$RiskRecipient==.025&dat_long$RiskDictator==.025&
                     dat_long$ReplacementRecipient==.2&dat_long$ReplacementDictator==.2]<-4

dat_long$situation[dat_long$total==9400&dat_long$RiskRecipient==.025&dat_long$RiskDictator==.025&
                     dat_long$ReplacementRecipient==.8&dat_long$ReplacementDictator==.8]<-5

dat_long$situation[dat_long$total==9400&dat_long$RiskRecipient==.2&dat_long$RiskDictator==.2&
                     dat_long$ReplacementRecipient==.2&dat_long$ReplacementDictator==.2]<-6

dat_long$situation[dat_long$total==9400&dat_long$RiskRecipient==.2&dat_long$RiskDictator==.2&
                     dat_long$ReplacementRecipient==.2&dat_long$ReplacementDictator==.8]<-7

dat_long$situation[dat_long$total==9400&dat_long$RiskRecipient==.2&dat_long$RiskDictator==.2&
                     dat_long$ReplacementRecipient==.8&dat_long$ReplacementDictator==.8]<-8

dat_long$situation[dat_long$total==9400&dat_long$RiskRecipient==.2&dat_long$RiskDictator==.2&
                     dat_long$ReplacementRecipient==.8&dat_long$ReplacementDictator==.2]<-9

dat_long$situation[dat_long$total==9400&dat_long$RiskRecipient==0&dat_long$RiskDictator==0&
                     dat_long$ReplacementRecipient==1&dat_long$ReplacementDictator==1]<-10

dat_long$situation[dat_long$total==13450&dat_long$RiskRecipient==.2&dat_long$RiskDictator==.2&
                     dat_long$ReplacementRecipient==.2&dat_long$ReplacementDictator==.2]<-11

dat_long$situation[dat_long$total==13450&dat_long$RiskRecipient==.2&dat_long$RiskDictator==.2&
                     dat_long$ReplacementRecipient==.2&dat_long$ReplacementDictator==.8]<-12

dat_long$situation[dat_long$total==13450&dat_long$RiskRecipient==.2&dat_long$RiskDictator==.2&
                     dat_long$ReplacementRecipient==.8&dat_long$ReplacementDictator==.2]<-13

dat_long$situation[dat_long$total==13450&dat_long$RiskRecipient==0&dat_long$RiskDictator==0&
                     dat_long$ReplacementRecipient==1&dat_long$ReplacementDictator==1]<-14

dat_long$situation[dat_long$total==9400&dat_long$RiskRecipient==.2&dat_long$RiskDictator==.025&
                     dat_long$ReplacementRecipient==.2&dat_long$ReplacementDictator==.8]<-15

dat_long$situation[dat_long$total==13450&dat_long$RiskRecipient==.2&dat_long$RiskDictator==.025&
                     dat_long$ReplacementRecipient==.2&dat_long$ReplacementDictator==.2]<-16

dat_long$situation
##########################


#Risk aversion type
table(dat_long$risk_1==1&dat_long$risk_2==1&
  dat_long$risk_3==1&dat_long$risk_4==1&
  dat_long$risk_5==1&dat_long$risk_6==1&
  dat_long$risk_7==1&dat_long$risk_8==1
&dat_long$risk_9==1)

table(dat_long$risk_aversion)
dat_long$risk_aversion<-NA
dat_long$risk_aversion[dat_long$risk_1==2&dat_long$risk_2==2&
                         dat_long$risk_3==2&dat_long$risk_4==2&
                         dat_long$risk_5==2&dat_long$risk_6==2&
                         dat_long$risk_7==2&dat_long$risk_8==2
                       &dat_long$risk_9==2]<--1.72
dat_long$risk_aversion[dat_long$risk_1==1&dat_long$risk_2==2&
                         dat_long$risk_3==2&dat_long$risk_4==2&
                         dat_long$risk_5==2&dat_long$risk_6==2&
                         dat_long$risk_7==2&dat_long$risk_8==2
                       &dat_long$risk_9==2]<--.95
dat_long$risk_aversion[dat_long$risk_1==1&dat_long$risk_2==1&
                         dat_long$risk_3==2&dat_long$risk_4==2&
                         dat_long$risk_5==2&dat_long$risk_6==2&
                         dat_long$risk_7==2&dat_long$risk_8==2
                       &dat_long$risk_9==2]<--.49
dat_long$risk_aversion[dat_long$risk_1==1&dat_long$risk_2==1&
                         dat_long$risk_3==1&dat_long$risk_4==2&
                         dat_long$risk_5==2&dat_long$risk_6==2&
                         dat_long$risk_7==2&dat_long$risk_8==2
                       &dat_long$risk_9==2]<--.15
dat_long$risk_aversion[dat_long$risk_1==1&dat_long$risk_2==1&
                         dat_long$risk_3==1&dat_long$risk_4==1&
                         dat_long$risk_5==2&dat_long$risk_6==2&
                         dat_long$risk_7==2&dat_long$risk_8==2
                       &dat_long$risk_9==2]<-.15
dat_long$risk_aversion[dat_long$risk_1==1&dat_long$risk_2==1&
                         dat_long$risk_3==1&dat_long$risk_4==1&
                         dat_long$risk_5==1&dat_long$risk_6==2&
                         dat_long$risk_7==2&dat_long$risk_8==2
                       &dat_long$risk_9==2]<-.41
dat_long$risk_aversion[dat_long$risk_1==1&dat_long$risk_2==1&
                         dat_long$risk_3==1&dat_long$risk_4==1&
                         dat_long$risk_5==1&dat_long$risk_6==1&
                         dat_long$risk_7==2&dat_long$risk_8==2
                       &dat_long$risk_9==2]<-.68
dat_long$risk_aversion[dat_long$risk_1==1&dat_long$risk_2==1&
                         dat_long$risk_3==1&dat_long$risk_4==1&
                         dat_long$risk_5==1&dat_long$risk_6==1&
                         dat_long$risk_7==1&dat_long$risk_8==2
                       &dat_long$risk_9==2]<-.97
dat_long$risk_aversion[dat_long$risk_1==1&dat_long$risk_2==1&
                         dat_long$risk_3==1&dat_long$risk_4==1&
                         dat_long$risk_5==1&dat_long$risk_6==1&
                         dat_long$risk_7==1&dat_long$risk_8==1
                       &dat_long$risk_9==2]<-1.37
dat_long$risk_aversion[dat_long$risk_1==1&dat_long$risk_2==1&
                         dat_long$risk_3==1&dat_long$risk_4==1&
                         dat_long$risk_5==1&dat_long$risk_6==1&
                         dat_long$risk_7==1&dat_long$risk_8==1
                       &dat_long$risk_9==1]<-4
##########################


##########################
#Save data
##########################

save(dat_long,file="dataLab_SocialInsurance.Rda")
############################




